$(function() {
	var innerID;
	var convertRate;
	var url = ctxPath + "/sys/user/getUser.do";
	var data = {};
	var rootSite = "http://www.hth168.com/amazoncn.php";
	doGetReq(url, data, function(rsp) {
		var retData = rsp.retData;
		innerID = retData.innerID;
	});
	
	$("#normal").keyup(function(){
		$("#final").html("");
	});
	
	$("#test2").bind("click", function() {
		var normal = $("#normal").val();
		var pid = convertStandard(normal);
		if (pid == null) {
			return;
		}
		pid = pid[0];
		var currentTime = Math.round(new Date().getTime()/1000);
		// 转换既设置cookie
		var label = $.cookie('ORDERINFO');
		if(typeof(label) === "undefined") {
			$.cookie('ORDERINFO', pid, {expires: 7});
		} else {
			var temp = label.split(",");
			temp.push(pid);
			$.unique(temp);
			label = temp.toString();
			$.cookie('ORDERINFO', label, {expires: 7});
		}
		var finalSite = convertFinal(rootSite, currentTime, innerID, pid);
		if (finalSite == null) {
			return;
		}
		$("#final").html("<a href='"+finalSite+"' target='_blank'><img src='http://file.chanet.com.cn/image.cgi?a=526788&d=4221&u="+innerID+"&e="+currentTime+"' width='0' height='0' border='0'><h4>点击这里前往中国亚马逊购买商品或把商品加入购物车</h4></a>");
		$("#alertInfo").html("请买家5分钟内完成购买完并登记！！！");
	});

	$("#clear2").bind("click", function() {
		$("#normal").val("");
		$("#final").html("");
		$("#alertInfo").html("");
		
	});
});

function convertStandard(source) {
	if ($.trim(source).length === 0) {
		return null;
	}
	var pattern = new RegExp("B0[A-Z0-9]{8}");
	return pattern.exec(source);
}

function convertFinal(rootSite, currentTime, innerID, pid) {
	if ($.trim(rootSite).length === 0 || $.trim(innerID).length === 0 ) {
		return null;
	}
	return rootSite + "?uid="+innerID+"&eid="+currentTime+"&pid="+pid;
}